﻿using LineFocus.Nikcron.Common;
using LineFocus.Nikcron.Models;
using LineFocus.Nikcron.Session;
using Newtonsoft.Json;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;


namespace LineFocus.Nikcron.Controllers
{
    public class UsersController : BaseController
    {
        //
        // GET: /Users/

        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Header = "User";
            ViewBag.Caption = "Listing";
            return View();
        }

        //[HttpGet]
        //public JsonResult GetUsers()
        //{
        //    ApplicationDBContext db = new ApplicationDBContext();
        //    var Users = from u in db.Offices.OfType<User>().ToList() 
        //                select new
        //                {
        //                    UserId = u.Id,
        //                    FirstName = u.FirstName,
        //                    LastName = u.LastName,
        //                    Gender = u.Gender,
        //                    AddressLine1 = u.PresentAddress,
        //                    AddressLine2 = u.PermanentAddress,
        //                    Mobile = u.Contact.Mobile,
        //                    Phone = u.Contact.Phone,
        //                    Email = u.Contact.Email,
        //                };
        //    JsonResult result = new JsonResult();
        //    result.Data = Users;
        //    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        //    return result;                           
        //}

        public ActionResult UserMaintenance()
        {
            //ApplicationDBContext db = new ApplicationDBContext();

            //ViewBag.Header = "User";
       
            //if (Id.HasValue)
            //{
            //    ViewBag.Id = Id;
            //    ViewBag.Caption = "Edit";

            //    User User = db.Offices.OfType<User>().Where(s => s.Id == Id).FirstOrDefault();
            //    return View(User);
            //}
            //else
            //{   
            //    User Use = new User();                
            //    ViewBag.Caption = "New";
            //    return View(User);
            //}
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            string Username = formCollection["Login"];
            string Password = formCollection["Password"];
            if (db.People.OfType<User>().Any(u => u.Username == Username && u.Password == Password))
            {
                User User = db.People.OfType<User>().Where(u => u.Username == Username && u.Password == Password).FirstOrDefault();
                CustomPrincipalSerilizeModel serilizeModel = new CustomPrincipalSerilizeModel();
                serilizeModel.UserId = User.Id;
                serilizeModel.OfficeId = User.OfficeId;
                serilizeModel.RoleId = User.RoleId;
                serilizeModel.FirstName = User.FirstName;
                serilizeModel.LastName = User.LastName;
                serilizeModel.Mobile = User.Contact.Mobile;
                serilizeModel.Email = User.Contact.Email;
                serilizeModel.OfficeName = User.Office.Name;
                serilizeModel.RoleName = User.Role.Name;
                string userData = JsonConvert.SerializeObject(serilizeModel);
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                    1,
                    User.Id.ToString(),
                    DateTime.Now,
                    DateTime.Now.AddMinutes(45),
                    false, //pass here true, if you want to implement remember me functionality
                    userData);

                string encTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                Response.Cookies.Add(faCookie);
                return RedirectToAction("Index", "Home");
            }
            else
                return RedirectToAction("Login", "Users", new { ErrorCode = 1 });
        }
        //[HttpPost]
        //public ActionResult UserMaintenance(FormCollection formCollection)
        //{
        //    ApplicationDBContext db = new ApplicationDBContext();
           
        //    User user = new Nickron.Database.User();
        //    Int32 UserId = 0;
        //    Int32.TryParse(formCollection["Id"], out UserId);
        //    if (UserId == 0)
        //    {
        //        user = new User();
        //        user.
               
        //    }
        //    else
               
        //    return RedirectToAction("Index");
        //}

    }
}
